-- 计算魔法伤害
-- percent为提升的千分比威力
-- point为提升的魔法点数

-- 计算一个人被攻击的受创值
local function MAGIC_DAMAGE_ONE(source, target, attack, percent, point)
    local sources = FormulaM.getPropTargets(source);
    local targets = FormulaM.getPropTargets(target);
    
    -- 2: 魔力提升N%
    local prop1 = FormulaM.combineProp(sources, 2);
    
    -- 9~13: X属性出战英雄魔力提升N%
    local prop2 = FormulaM.combinePropByAttr(sources, source.attr, { 9, 10, 11, 12, 13 });
    
    -- 36~43: X系出战影响魔力提升N%
    local prop3 = FormulaM.combinePropByStyle(sources, source.style, 
                                              { 36, 36, 38, 39, 40, 41, 42, 43 });
    
    -- 百分比提升先计算
    prop1[2] = prop1[2] + prop2[2] + percent;
    attack = PropM.apply(prop1, attack);
    
    -- 301 : 魔力提升N点
    prop1 = FormulaM.combineProp(sources, 301);
    attack = PropM.apply(prop1, attack) + point;
    
    -- 308~312: X属性出战英雄魔力增加N点
    prop1 = FormulaM.combinePropByAttr(sources, source.attr, { 308, 309, 310, 311, 312 });
    attack = PropM.apply(prop1, attack);
    
    -- 338~342: 目标是X属性时，伤害增加N点
    prop1 = FormulaM.combinePropByAttr(sources, target.attr, 
                                       { 338, 339, 340, 341, 342 }, false);
    attack = PropM.apply(prop1, attack);
    
    -- 337: 魔力减低N点
    prop3 = FormulaM.combineProp(sources, 337);
    attack = PropM.apply(prop3, attack);
    
    -- 到这里，得到了攻击者因为附加属性影响后的攻击值
    -- 然后开始防守者的附加属性影响
    
    -- 19: 伤害减少N%
    prop3 = FormulaM.combineProp(targets, 19);
    attack = PropM.apply(prop3, attack);
    
    -- 318: 伤害减少N点
    prop3 = FormulaM.combineProp(targets, 318);
    attack = PropM.apply(prop3, attack);
    
    -- 319~323: X属性出战英雄伤害减少N点
    prop1 = FormulaM.combinePropByAttr(targets, target.attr, 
                                       { 319, 320, 321, 322, 323 }, false);
    attack = PropM.apply(prop1, attack);
    
    -- 计算完毕
    return attack;
end

return function(source, target, attack, percent, point)
    -- 如果是我攻击怪物，那么比较简单，目标target即为怪物一个人
    if (target.type == OBJECT_TYPE_MONSTER) then
        return MAGIC_DAMAGE_ONE(source, target, attack, percent, point);
    end
    
    -- 如果是怪物攻击我，那么target就应该是所有宠物作为一个整体来运算了
    local damage = 0;
    for _, f in _G.templet:getFriends(target) do
        if (f.type ~= OBJECT_TYPE_USER) then
            damage = damage + MAGIC_DAMAGE_ONE(source, f, attack, percent, point);
        end
    end
    
    return damage;
end
